package org.eenie.appseed.ui.chat.logic

import androidx.lifecycle.rxLifeScope
import kotlinx.coroutines.delay
import org.eenie.appseed.ui.chat.models.im.BaseMsg
import org.eenie.appseed.ui.chat.models.im.TextMsg
import org.eenie.core.base.viewmodel.BaseViewModel
import org.eenie.core.ext.launchWithDialog
import org.eenie.core.ext.lifecycleScope
import kotlin.random.Random

class ChatViewModel : BaseViewModel<ChatViewState>(ChatViewState.InitState()) {

    val msgList = mutableListOf<BaseMsg>()

    fun fetchMsgList(isRefresh: Boolean = true, isFirst: Boolean = true) {
        lifecycleScope.launchWithDialog {
            delay(1000)
            val list = if (isRefresh) {
                (1..20).map {
                    TextMsg().apply {
                        text = "这个是刷新的消息$it"
                        isSelf = Random.nextInt() % 2 == 0
                    }
                }
            } else {
                (1..20).map {
                    TextMsg().apply {
                        text = "这个是加载的消息$it"
                        isSelf = Random.nextInt() % 2 == 0
                    }
                }
            }
            viewState = ChatViewState.FetchMsgListSuccess(if (isRefresh) 0 else 1, list, isFirst)
        }
    }

}